package com.rent.spider;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/10/22

*/
public class AnjukeThread extends Thread {
    String string;
    Document doc;

    AnjukeThread(String str,Document docc){
        string = str;
        doc = docc;
    }


    public static void main(String[] args) throws IOException {
            String proxyHost = "127.0.0.1";
            String proxyPort = "11478";
            System.setProperty("http.proxyHost", proxyHost);
            System.setProperty("http.proxyPort", proxyPort);
            // 对https也开启代理
            System.setProperty("https.proxyHost", proxyHost);
            System.setProperty("https.proxyPort", proxyPort);  //设置代理来访问豆瓣

            String link = "https://nj.zu.anjuke.com/ditie/p4/";
            Connection connect = Jsoup.connect(link);
            Document doc = connect.get();
        Elements elements = doc.getElementsByClass("zu-itemmod");
        for (Element element : elements) {
            System.out.println(Html2Text.getContent(element.getElementsByClass("details-item tag").html()));
        }

    }

    private static Logger logger = Logger.getLogger(WebTheadfangtianxia.class);

    synchronized public void run() {
        try {
            Elements listCon = doc.getElementsByClass("zu-itemmod");

            for (Element element : listCon) {
                try{

                Thread.sleep(3000);
                //获取房子的图片链接
                String imgUrl = element.getElementsByTag("img").attr("lazy_src");
                //获取房子的具体信息链接
                String houseSingleUrl = element.attr("link");
                //房子的标题
                String title = Html2Text.getContent(element.getElementsByTag("h3").html());

                String replace = Html2Text.getContent(element.getElementsByClass("details-item tag").html());
                String[] info = replace.split("\\|");

                String replace1 = Html2Text.getContent(element.getElementsByClass("details-item bot-tag").html());
                String[] info1 = replace1.split(" ");
                //获取房型：1室2厅
                String singleType = "";
                //获取面积
                String size = "";
                //获取精装，简装
                String direcType = "";
                //获取楼层
                String singleLayer = "";
                //整租，合租，出租类型
                String rentType = "";
                //获取房型
                singleType = info[0];
                //获取面积
                size = info[1];
                //获取装修类型
                direcType = "暂无详情";

                //获取出租类型
                singleLayer = info[2].split(" ")[0];

                rentType = info1[0];

                //秦淮-月牙湖-天坛新村^空房
                Thread.sleep(5000);
                String place = Html2Text.getContent(element.getElementsByTag("address").html()).split("   ")[1];
                //南京市月牙湖小学
                Elements elementsByClass = element.getElementsByClass("ad-item-fang-tags");
                String underPlace = "";
                underPlace = info1[info1.length-1];

                //房子的价格
                String price = Html2Text.getContent(element.getElementsByClass("zu-side").html());
                //输出信息到日志
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(imgUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(houseSingleUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(title.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(singleType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(size.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(direcType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(singleLayer.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(rentType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(place.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(underPlace.replace("\t","").replace("\r","").replace("\n","")).append("^")
                        .append(price.replace("\t","").replace("\r","").replace("\n",""));
                logger.info(stringBuilder.toString());
                }catch (IndexOutOfBoundsException e){
                   continue;
                }
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }



    }
}
